package com.cloud.code.单例模式.静态内部类单例;

import lombok.SneakyThrows;

import java.lang.reflect.Constructor;

/**
 * 反射和反序列化都会破解单例
 */
public class Test {

    @SneakyThrows
    public static void main(String[] args) {



        //利用反射 破解单例模式
        Class<User> userClass = User.class;
        Constructor<User> declaredConstructor = userClass.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        User user = declaredConstructor.newInstance();
        User u = User.getInstance();
        System.out.println(u == user);
    }
}
